package net.xdclass.controller;

import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Classname NotifyController
 * @Description TODO
 * @Date 2022/4/7 9:54
 * @Created by sunhao
 */
@Slf4j
@Api(tags = "通知模块")
@RestController
@RequestMapping("/api/notify/v1")
public class NotifyController {

  @Autowired
  private Producer captchaProducer;

  @Autowired
  private StringRedisTemplate redisTemplate;

  @Autowired
  private NotifyService notifyService;

  /**
   *临时使用10分钟有效，方便测试
   */
  private static final long CAPTCHA_CODE_EXPIRED = 60 * 1000 * 10;


  /**
   * 获取图形验证码
   */
  @ApiOperation(value = "获取图形验证码")
  @GetMapping("/captcha")
  public void captcha(HttpServletRequest request, HttpServletResponse response){
    String captchaText = captchaProducer.createText();
    log.info("图形验证码{}",captchaText);
    //存储
    redisTemplate.opsForValue().set(getCaptchaKey(request),captchaText,CAPTCHA_CODE_EXPIRED, TimeUnit.MILLISECONDS);
    BufferedImage bufferedImage = captchaProducer.createImage(captchaText);
    ServletOutputStream outputStream = null;
    try {
      outputStream = response.getOutputStream();
      ImageIO.write(bufferedImage,"jpg",outputStream);
      outputStream.flush();
      outputStream.close();
    } catch (IOException e) {
      log.info("获取图形验证码异常{}",e);
    }
  }

  /**
   * 发送邮箱验证码
   * @param to
   * @param captcha
   * @param request
   * @return
   */
 @ApiOperation(value = "发送邮箱验证码")
 @GetMapping("/send_register_code")
 public JsonData  sendRegisterCode(@ApiParam(value = "发送的地址",required = true) @RequestParam("to")String to,@ApiParam(value = "图形验证码",required = true) @RequestParam("captcha") String captcha, HttpServletRequest request){
   String captchaKey = getCaptchaKey(request);
   String captchaText = redisTemplate.opsForValue().get(captchaKey);
   if(StringUtils.isEmpty(captchaText)){
       return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_NOT_EXIT);
   }
   if(!captchaText.equalsIgnoreCase(captcha)){
     return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA);
   }
   redisTemplate.delete(captchaKey);
   return notifyService.sendCode(SendCodeEnum.USER_REGISTER,to);
 }


  private String getCaptchaKey(HttpServletRequest request){
    String ip = CommonUtil.getIpAddr(request);
    String userAgent = request.getHeader("user-agent");
    log.info("ip={}",ip);
    log.info("userAgent={}",userAgent);
    return  "user-server:captcha:"+CommonUtil.MD5(ip+userAgent);
  }
}
